/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LoginStaffManager;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alex
 */
public class LoginStaffManagerServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LoginStaffManagerServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet LoginStaffManagerServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
            
            HttpSession session = request.getSession(false);
            
            String username;
            String password;
            boolean checker;

            username = request.getParameter("manager");
            password = request.getParameter("manpass");

            //Assuming we have a function that checks the user credentials
            checker = new LoginStaffManager().Log(username, password);

            if(checker == true){
                String managerid  = new LoginStaffManager().getInfo(username, "MangId");
                String manager = new LoginStaffManager().getInfo(username, "username");
              
                String firstname  = new LoginStaffManager().getInfo(username, "firstname");
                String middlename  = new LoginStaffManager().getInfo(username, "middle");
                String lastname  = new LoginStaffManager().getInfo(username, "lastname");
                String usertype = new LoginStaffManager().getInfo(username, "usertype");
                String type = new LoginStaffManager().getInfo(username, "type");
               
            
            
               
                //new LoginManager().updateLastLog(username);
                
                session.setAttribute("managerid", managerid);                     
                session.setAttribute("username",manager);
               // session.setAttribute("password",manpass);        
                session.setAttribute("firstname",firstname);             
                session.setAttribute("middlename",middlename);             
                session.setAttribute("lastname",lastname);   
                session.setAttribute("usertype", usertype);
                
            
                
            /*    if(usertype.equals("1"))
                    response.sendRedirect("customer.jsp");
                else if(usertype.equals("2"))
                    response.sendRedirect("login1234foobar.jsp");
                else*/ 
            if(checker == true)
                    response.sendRedirect("productmanagerbook.jsp");
            
            } 
            
            else
             {
              response.sendRedirect("index.jsp");
             }
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
